﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

using Aspose.Words;

namespace Best.Framework.Web
{
    public class WordHelper
    {
        private static List<string> _wordFileExtList = new List<string> { 
            ".doc", ".docx"
        };

        /// <summary>
        /// 读取 Word 文本内容
        /// </summary>
        /// <param name="wordFilePath">Word文件路径</param>
        /// <returns>纯文本返回</returns>
        public static string ReadAllText(string wordFilePath)
        {
            string wordText = string.Empty;
            if (string.IsNullOrEmpty(wordFilePath))
            {
                throw new ArgumentNullException("参数 wordFilePath 不能为空");
            }
            if (!File.Exists(wordFilePath))
            {
                throw new ArgumentException("文件不存在，请确认路径是否正确");
            }
            string fileExt = Path.GetExtension(wordFilePath);

            if (!_wordFileExtList.Contains(fileExt))
            {
                throw new ArgumentException("不支持该文件格式");
            }
            Document wordDoc = new Document(wordFilePath);
            wordText = wordDoc.GetText();
            return wordText;
        }

        /// <summary>
        /// 读取 Word 文本内容
        /// </summary>
        /// <param name="wordFilePath">Word文件路径</param>
        /// <param name="tempImageFileFolderPath">存放图片的临时文件路径</param>
        /// <returns>以Html格式返回，包含Html标签</returns>
        public static string ReadAllHtmlText(string wordFilePath, string tempImageFileFolderPath)
        {
            string wordText = string.Empty;
            if (string.IsNullOrEmpty(wordFilePath))
            {
                throw new ArgumentNullException("参数 wordFilePath 不能为空");
            }
            if (!File.Exists(wordFilePath))
            {
                throw new ArgumentException("文件不存在，请确认路径是否正确");
            }
            string fileExt = Path.GetExtension(wordFilePath);

            if (!_wordFileExtList.Contains(fileExt))
            {
                throw new ArgumentException("不支持该文件格式");
            }

            if (string.IsNullOrEmpty(tempImageFileFolderPath))
            {
                throw new ArgumentNullException("参数 tempImageFileFolderPath 不能为空");
            }
            if (!Directory.Exists(tempImageFileFolderPath))
            {
                throw new ArgumentException("目录不存在，请确认路径是否正确");
            }
            Document wordDoc = new Document(wordFilePath);
            MemoryStream ms = new MemoryStream();
            wordDoc.SaveOptions.HtmlExportImagesFolder = tempImageFileFolderPath;
            wordDoc.Save(ms, SaveFormat.Html);
            wordText = Encoding.UTF8.GetString(ms.ToArray());
            ms.Close();

            return wordText;
        }

        /// <summary>
        /// 填充模版文件书签并保存到指定文件
        /// </summary>
        /// <param name="moduleFilePath">模版文件路径</param>
        /// <param name="saveFilePath">保存文件路径</param>
        /// <param name="bookmarkValueDict">书签名-值字典</param>
        public static void WriteBookmarkToWord(string moduleFilePath, string saveFilePath, IDictionary<string, object> bookmarkValueDict)
        {
            if (string.IsNullOrEmpty(moduleFilePath))
            {
                throw new ArgumentNullException("参数 moduleFilePath 不能为空");
            }
            if (!File.Exists(moduleFilePath))
            {
                throw new FileNotFoundException("文件不存在");
            }

            if (!_wordFileExtList.Contains(Path.GetExtension(moduleFilePath)))
            {
                throw new ArgumentException("不支持该文件格式");
            }
            if (string.IsNullOrEmpty(saveFilePath))
            {
                throw new ArgumentNullException("参数 saveFilePath 不能为空");
            }

            Document doc = new Document(moduleFilePath);

            foreach (KeyValuePair<string, object> item in bookmarkValueDict)
            {
                doc.Range.Bookmarks[item.Key].Text = item.Value.ToString();
            }

            doc.Save(saveFilePath, SaveFormat.Doc );
        }
    }
}
